package br.com.jogo.screens;

import br.com.jogo.MagesGame;
import br.com.jogo.neg.BattleField;
import br.com.jogo.neg.Tile;
import br.com.jogo.neg.Unit;
import br.com.jogo.neg.actions.MoveAction;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class BattleScreen extends BaseScreen {

	public BattleField battleField = new BattleField();
	public Unit selectedUnit;
	public Tile selectedTile;

	public BattleScreen(MagesGame game) {
		super(game);
	}

	@Override
	/** Called when this screen becomes the current screen. */
	public void show() {

		battleField.teamA.addUnit(new Unit(5, 10));
		battleField.teamA.addUnit(new Unit(5, 10 + 50));
		battleField.teamB.addUnit(new Unit(5 + 40 * 10, 10 + 50));
	}

	@Override
	public void update(float delta) {

		battleField.update(delta);

		battleField.teamA.update(delta);
		battleField.teamB.update(delta);

	}

	@Override
	public void draw(SpriteBatch spritebatch, float delta) {

		spritebatch.begin();

		battleField.drawBackground(spritebatch, delta);
		battleField.drawTeams(spritebatch, delta);

		spritebatch.end();
	}

	@Override
	public void dispose() {

	}

	@Override
	public boolean onTouchUp(float x, float y, int pointer, int button) {

		if (selectedTile != null) {
			Tile destination = this.battleField.getTileAtPoint(x, y);

			if (selectedUnit != null && selectedUnit.idle()) {

				if (destination != null && destination.element == null) {

					if (destination.x == selectedTile.x) { // Same column

						if (destination.y == selectedTile.y - 1) {
							Gdx.app.log(MagesGame.LOG, "move");

							selectedUnit.addAction(new MoveAction(
									selectedUnit.bounds.x,
									selectedUnit.bounds.y - Tile.TILE_HEIGHT));

						} else if (destination.y == selectedTile.y + 1) {
							Gdx.app.log(MagesGame.LOG, "move");

							selectedUnit.addAction(new MoveAction(
									selectedUnit.bounds.x,
									selectedUnit.bounds.y + Tile.TILE_HEIGHT));
						}
					}

					if (destination.y == selectedTile.y) { // Same row

						if (destination.x == selectedTile.x + 1) {
							Gdx.app.log(MagesGame.LOG, "move");

							selectedUnit.addAction(new MoveAction(
									selectedUnit.bounds.x + Tile.TILE_WIDTH,
									selectedUnit.bounds.y));
						} else if (destination.x == selectedTile.x - 1) {

							Gdx.app.log(MagesGame.LOG, "move");

							selectedUnit.addAction(new MoveAction(
									selectedUnit.bounds.x - Tile.TILE_WIDTH,
									selectedUnit.bounds.y));
						}

					}

				}

			}

		}

		selectedUnit = null;
		return true;

	}

	@Override
	public boolean onTouchDown(float x, float y, int pointer, int button) {
		selectedUnit = null;
		selectedTile = null;

		for (int i = 0; i < battleField.teamA.units.size(); i++) {
			if (battleField.teamA.units.get(i).bounds.contains(x, y)) {
				selectedUnit = battleField.teamA.units.get(i);
				selectedTile = battleField.getTileAtPoint(x, y);
				break;
			}
		}

		Gdx.app.log(MagesGame.LOG, "selected: " + (selectedUnit != null));

		return true;
	}

	@Override
	public boolean onTouchDragged(float x, float y, int pointer) {

		return false;
	}
}